package com.rzjm.api.util;

/**
 * @创建者：zhouwei
 * @创建时间：2024-07-26
 * @描述：
 */
public class ByteUtil {

    public static void main(String[] args) {

//        String ss1 = "ffff937541d7";
//        ss1 = ss1.replaceAll("f","");
//        System.out.println("ss1: " + ss1);
//
//
////
//        int ff = -5000;
//
//        String left = String.valueOf(ff);
//        System.out.println(left.replace(".",""));
//        String sss = left.replace(".","");
//        int num = Integer.parseInt(sss);
//        System.out.println("num = " + num);
//
//
//        int number = 16855;
//        String hexString = Integer.toHexString(ff);
//        System.out.println("16进制表示: " + hexString);
//
//        float f = hexToFloat("41d79375");
//        System.out.println("转换后的数值: " + f);



        String hexString = "02C9";
        int decimal = Integer.parseInt(hexString, 16);
        System.out.println("16进制数: " + hexString + " 对应的10进制数: " + decimal);




//        double fff = heBingLeftAndRight("1709.5","2431.4");
//        System.out.println("fff : " + fff);
    }

    public static float heBingLeftAndRight(String left, String right){

        String totalHex = "";
//        String leftStr = String.valueOf(left);
//        System.out.println(leftStr.replace(".",""));
        String leftStr = left.replace(".","");
        int leftNum = Integer.parseInt(leftStr);
        String leftHexString = Integer.toHexString(leftNum).replaceAll("ffff","");
        System.out.println("leftHexString = " + leftHexString);


//        String rightStr = String.valueOf(right);
//        System.out.println(leftStr.replace(".",""));
        String rightStr = right.replace(".","");
        int rightNum = Integer.parseInt(rightStr);

        String rightHexString = Integer.toHexString(rightNum).replaceAll("ffff","");
        System.out.println("rightHexString = " + rightHexString);

        totalHex = leftHexString + rightHexString;
        totalHex = totalHex;
        System.out.println("totalHex = " + totalHex);

        float f = hexToFloat(totalHex);
        System.out.println("result = " + f);
        return f;

    }


    /**
     *  16进制数据转换成int后，除以 10
     * @param hex
     * @return
     */
    public static double bytesToDouble(String hex){
        double d = 0d;
        String hexString = hex;
        int decimal = Integer.parseInt(hexString, 16);
        System.out.println("16进制数: " + hexString + " 对应的10进制数: " + decimal);
        d = MathUtil.getDoubleUp(decimal / 10);
        return d;
    }


    public static double heBingLeftAndRightDouble(String left, String right){

        String totalHex = "";
//        String leftStr = String.valueOf(left);
//        System.out.println(leftStr.replace(".",""));
        String leftStr = left.replace(".","");
        int leftNum = Integer.parseInt(leftStr);
        String leftHexString = Integer.toHexString(leftNum).replaceAll("ffff","");
        System.out.println("leftHexString = " + leftHexString);


//        String rightStr = String.valueOf(right);
//        System.out.println(leftStr.replace(".",""));
        String rightStr = right.replace(".","");
        int rightNum = Integer.parseInt(rightStr);

        String rightHexString = Integer.toHexString(rightNum).replaceAll("ffff","");
        System.out.println("rightHexString = " + rightHexString);

        totalHex = leftHexString + rightHexString;
        totalHex = totalHex;
        System.out.println("totalHex = " + totalHex);

        double f = hexToDouble(totalHex);
        System.out.println("result = " + f);
        return f;

    }

    /**
     *  int 转16进制
     * @param num
     * @return
     */
    public static String intToHex(int num){
//        int number = num;
        String hexString = Integer.toHexString(num);
        System.out.println("16进制表示: " + hexString);
        return hexString.toUpperCase();
    }


    public static float hexToFloat(String hex){
        int num = Integer.parseInt(hex, 16);
        float floatValue = Float.intBitsToFloat(num);
        return floatValue;
    }



    public static double hexToDouble(String hex){
//        int num = Integer.parseInt(hex, 16);
//        float floatValue = Float.intBitsToFloat(num);

//        return floatValue;
        long num = Long.parseLong(hex, 16);
        double d = Double.longBitsToDouble(num);

        return d;

    }


}
